package com.lv.service.impl;

import com.lv.mapper.TbBrandMapper;
import com.lv.model.TbBrand;
import com.lv.model.TbBrandExample;
import com.lv.service.BrandService;
import com.lv.vo.BrandVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private TbBrandMapper bm;


    //查询

    @Override
    public List<BrandVo> brandList(BrandVo brandVo) {
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(brandVo.getFirstChar())){
            criteria.andFirstCharEqualTo(brandVo.getFirstChar());
        }
        if (StringUtils.isNoneBlank(brandVo.getName())){
            criteria.andNameLike("%"+brandVo.getName()+"%");
        }

        List<TbBrand> tbBrands = bm.selectByExample(example);

        return tbBrands.stream().map(brand ->{
            BrandVo vo = new BrandVo();
            BeanUtils.copyProperties(brand,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    //回显

    @Override
    public BrandVo toupdate(Long id) {
        TbBrand tbBrand = bm.selectByPrimaryKey(id);
        BrandVo vo = new BrandVo();
        BeanUtils.copyProperties(tbBrand,vo);
        return vo;
    }

    //修改

    @Override
    public void update(BrandVo vo) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(vo,brand);
        bm.updateByPrimaryKeySelective(brand);
    }

    //增加

    @Override
    public void add(BrandVo vo) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(vo,brand);
        bm.insert(brand);
    }

    //删除

    @Override
    public void delete(Long id) {
        bm.deleteByPrimaryKey(id);
    }
}
